package org.example.code.operation;

import org.example.code.entity.DailyGoods;
import org.example.code.entity.EatingGoods;
import org.example.code.entity.Goods;

import java.math.BigDecimal;

import java.util.InputMismatchException;
import java.util.List;

import java.util.Scanner;

public class Operation {
    Check check = new Check();
    //添加商品
    public Goods addGoods(List<Goods> list){

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品类别");
        String category = sc.next();

        Integer id;
        Integer quantitys;
        BigDecimal prices;

        if("food".equals(category)){
            Boolean yammy;
            System.out.println("请输入食品id");
            String ID = sc.next();
            try{
                 id = Integer.parseInt(ID);
            }catch(NumberFormatException e){
                System.out.println("请输入正确的ID");
                return null;
            }
            if(!check.idCheck(list,id)){
                return null;
            }

            System.out.println("请输入食品名称");
            String name = sc.next();
            if(name.isEmpty() || name.length() > 10){
                System.out.println("格式错误");
                return null;
            }
            if(!check.nameCheck(list,name)){
                return null;
            }

            check.nameCheck(list,name);
            System.out.println("请输入食品数量");
            String quantity = sc.next();
            try{
                quantitys = Integer.parseInt(quantity);
            }catch(NumberFormatException e){
                System.out.println("请输入正确的数量");
                return null;
            }

            System.out.println("请输入食品价格");
            String price = sc.next();
            try{
                prices =  new BigDecimal(price);
            }catch (NumberFormatException e){
                System.out.println("请输入正确的价格");
                return null;
            }

            System.out.println("请输入是否好吃");
            try{
                yammy = sc.nextBoolean();
            }catch (InputMismatchException e){
                System.out.println("bool值哦");
                return null;
            }

            Goods eg = new EatingGoods(id,name,quantitys,prices,category,yammy);
            return eg;
        }
        if("daily".equals(category)){
            Integer consistences;
            System.out.println("请输入日用品id");
            String ID = sc.next();
            try{
                id = Integer.parseInt(ID);
            }catch(NumberFormatException e){
                System.out.println("请输入正确的ID");
                return null;
            }
            if(!check.idCheck(list,id)){
                return null;
            }

            System.out.println("请输入日用品名称");
            String name = sc.next();
            if(name.isEmpty() || name.length() > 10){
                System.out.println("格式错误");
                return null;
            }
            if(!check.nameCheck(list,name)){
                return null;
            }

            System.out.println("请输入日用品数量");
            String quantity = sc.next();
            try{
                quantitys = Integer.parseInt(quantity);
            }catch(NumberFormatException e){
                System.out.println("请输入正确的数量");
                return null;
            }

            System.out.println("请输入日用品价格");
            String price = sc.next();
            try{
                prices =  new BigDecimal(price);
            }catch (NumberFormatException e){
                System.out.println("请输入正确的价格");
                return null;
            }

            System.out.println("请输入使用时间");
            String consistence = sc.next();
            try{
                consistences = Integer.parseInt(consistence);
            }catch (NumberFormatException e){
                System.out.println("请输入正确的结果");
                return null;
            }
            Goods dg = new DailyGoods(id,name,quantitys,prices,category,consistences);
            return dg;
        }
        System.out.println("查无此种");
        return null;
    }
    //查询库存
    public void goodsSelect(List<Goods> list){
        for (Goods goods : list) {
            if("food".equals(goods.getCategory())){
                System.out.print("ID:" + goods.getID() + " ");
                System.out.print("名称" + goods.getName() + " ");
                System.out.print("数量:" + goods.getQuantity() + " ");
                System.out.print("价格:" + goods.getPrice() + " ");
                System.out.print("种类:" + goods.getCategory() + " ");
                System.out.println("好吃吗:" + goods.yammyToString());
            }
            if("daily".equals(goods.getCategory())){
                System.out.print("ID:" + goods.getID() + " ");
                System.out.print("名称" + goods.getName() + " ");
                System.out.print("数量:" + goods.getQuantity() + " ");
                System.out.print("价格:" + goods.getPrice() + " ");
                System.out.print("种类:" + goods.getCategory() + " ");
                System.out.println("持续时间:" + goods.yammyToString());
            }
        }
    }

    public List<Goods> priceUpdate(List<Goods> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品id");

        String ID = sc.next();
        Integer id;
        BigDecimal prices;

        try{
            id = Integer.parseInt(ID);
        }catch(NumberFormatException e){
            System.out.println("请输入正确的ID");
            return null;
        }

        System.out.println("请输入价格");
        String price = sc.next();
        try{
            prices =  new BigDecimal(price);
        }catch (NumberFormatException e){
            System.out.println("请输入正确的价格");
            return null;
        }

        for (Goods goods : list) {
            if(id == goods.getID()){
                goods.setPrice(prices);
                System.out.println("修改成功！");
                return list;
            }
        }
        System.out.println("未查询到此商品");
        return null;
    }
}
